namespace iScadaFBD
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class ToolShape : iScadaFBD.Tool
    {
        private System.Windows.Forms.Cursor cursor;
        private Point lastPoint = new Point(0, 0);
        private Point startPoint = new Point(0, 0);

        public ToolShape(DrawArea drawArea)
        {
            drawArea.Cursor = Cursors.Cross;
        }

        protected void AddNewObject(DrawArea drawArea, DrawObject o)
        {
            drawArea.ObjList.UnselectAll();
            o.Selected = true;
            drawArea.ObjList.AddObject(o);
            drawArea.Capture = true;
            this.Cursor = Cursors.Default;
            drawArea.Refresh();
        }

        protected Rectangle GetNormalizedRectangle(Point startPoint, Point lastPoint)
        {
            int x;
            if (lastPoint.X < startPoint.X)
            {
                x = startPoint.X;
                startPoint.X = lastPoint.X;
                lastPoint.X = x;
            }
            if (lastPoint.Y < startPoint.Y)
            {
                x = startPoint.Y;
                startPoint.Y = lastPoint.Y;
                lastPoint.Y = x;
            }
            return new Rectangle(startPoint.X, startPoint.Y, lastPoint.X - startPoint.X, lastPoint.Y - startPoint.Y);
        }

        public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
        {
            Point point = new Point(e.X, e.Y);
            point = drawArea.ScalePoint(new Point(e.X, e.Y))[0];
            drawArea.Cursor = Cursors.Cross;
        }

        public override void OnMouseMove(DrawArea drawArea, MouseEventArgs e)
        {
            Point point = new Point(e.X, e.Y);
            point = drawArea.ScalePoint(new Point(e.X, e.Y))[0];
            drawArea.Cursor = Cursors.Cross;
            this.lastPoint.X = point.X;
            this.lastPoint.Y = point.Y;
            if (e.Button == MouseButtons.Left)
            {
                drawArea.Refresh();
            }
        }

        public override void OnMouseUp(DrawArea drawArea, MouseEventArgs e)
        {
            drawArea.ActivedFBType = GlobalParam.FBType.POINTER;
            drawArea.Capture = false;
            drawArea.Refresh();
        }

        protected System.Windows.Forms.Cursor Cursor
        {
            get
            {
                return this.cursor;
            }
            set
            {
                this.cursor = value;
            }
        }
    }
}

